package cn.rxmind.dlog.common;

import org.springframework.context.ApplicationListener;

import java.util.List;

/**
 *
 * @description 这里定义日志消费事件,子类实现可以使用线程池异步化
 *              Sender可以使用异步的实现
 *
 * @author renxin
 * @email renxinzhiliangzhi@163.com
 * @date 2020/7/10 1:33 下午
 */
public class DLogDataCanSendListener implements ApplicationListener<DLogDataCanSendEvent> {

    private DLogDataBuffer buffer;

    private DLogDataSender sender;

    public DLogDataCanSendListener(DLogDataBuffer buffer, DLogDataSender sender) {

        this.buffer = buffer;
        this.sender = sender;
    }

    @Override
    public void onApplicationEvent(DLogDataCanSendEvent dLogDataCanSendEvent) {

        List<DLogData> dLogData = buffer.batchTake(dLogDataCanSendEvent.getSendCount());
        sender.sender(dLogData);
    }
}
